﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Models;
using VietPremier.Repository;
using PagedList;
using System.Text;

namespace VietPremier.Controllers
{
    public class StyleController : Controller
    {
     
        private int pageSize = 20;

        //public ActionResult Style()
        //{
        //    using (dbVietPremierDataContext db = new dbVietPremierDataContext())
        //    {
        //        GetStyleModel model = new GetStyleModel();
        //        foreach (var style in db.EntityStyles.OrderBy(o => o.Position))
        //        {
        //            model.listStyle.Add(new Style()
        //            {
        //                Id = style.Id,
        //                Name = style.Name
        //            });
        //        }
        //        return View(model);
        //    }
        //}

        //#region "List Travel"
        //public ActionResult ListTravel(int idStyle, int? page)
        //{
        //    using (dbVietPremierDataContext db = new dbVietPremierDataContext())
        //    {
        //        ViewBag.IdStyle = idStyle;
        //        var query = from q in db.EntityStyles
        //                    join p in db.EntityTravelCities on q.Id equals p.IdStyle
        //                    where p.IdStyle == idStyle
        //                    select p;
        //        if (query != null)
        //        {
        //            List<EntityTravelCity> listCity = new List<EntityTravelCity>();
        //            listCity = query.ToList();

        //            int pageSize = 4;
        //            int pageNumber = (page ?? 1);
        //            return PartialView(listCity.ToPagedList(pageNumber, pageSize));

        //        }
        //        else
        //        {
        //            return View("Error");
        //        }
        //    }
        //}

        //[HttpPost]
        //public ActionResult AjaxListTravel(int idStyle, int? page)
        //{
        //    using (dbVietPremierDataContext db = new dbVietPremierDataContext())
        //    {
        //        ViewBag.IdStyle = idStyle;
        //        var query = from q in db.EntityStyles
        //                    join p in db.EntityTravelCities on q.Id equals p.IdStyle
        //                    where p.IdStyle == idStyle
        //                    select p;
        //        if (query != null)
        //        {
        //            List<EntityTravelCity> listCity = new List<EntityTravelCity>();
        //            listCity = query.ToList();

        //            int pageSize = 4;
        //            int pageNumber = (page ?? 1);
        //            return PartialView(listCity.ToPagedList(pageNumber, pageSize));

        //        }
        //        else
        //        {
        //            return View("Error");
        //        }
        //    }
        //}
        //#endregion

        #region "TravelStyle"
        public ActionResult TravelStyle(string title, int page = 1)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                int id = Utilities.GetIdByTitle(title, VietPremierMenu.TravelStyle, VietPremierDetailList.Detail);

                GetStyleModel model = new GetStyleModel();
                if (id > 0)
                {
                    EntityStyle style = db.EntityStyles.FirstOrDefault(o => o.Id == id);
                    model.style.Id = style.Id;
                    model.style.Name = style.Name;
                    model.style.Titletag = style.Titletag;
                    model.style.Meta_description = style.Meta_description;
                    model.style.Meta_keyword = style.Meta_keyword;
                    foreach (var travelStyle in db.EntityTravelStyles.Where(o => o.IdStyle == id).Skip((page - 1) * pageSize).Take(pageSize))
                    {
                        EntityTravelCity travelCity = db.EntityTravelCities.OrderBy(o => o.Position).FirstOrDefault(o => o.Id == travelStyle.IdTravelCity);
                        model.listTravelCity.Add(new TravelCity()
                        {
                            Id = travelCity.Id,
                            Image = travelCity.Image,
                            Name = travelCity.Name,
                            Price = travelCity.Price,
                            //TripCode = travelCity.TripCode,
                            Time = travelCity.Time,
                            Duration = travelCity.Duration
                        });
                    }
                    if (model.listTravelCity != null)
                    {
                        model.PagingInfo = new PagingInfo()
                            {
                                CurrentPage = page,
                                ItemsPerPage = pageSize,
                                TotalItems = db.EntityTravelStyles.Where(o => o.IdStyle == id).Count()
                            };
                        return View(model);
                    }
                    else
                    {
                        return View(model);
                    }
                }else{
                    return View();
                }              
            }
        }
        #endregion

        #region "TravelDetail"
        public ActionResult TravelDetail(string title, string city)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                int idTravelCity = Utilities.GetIdByTitle(city, VietPremierMenu.TravelCity, VietPremierDetailList.Detail);
                int idStyle = Utilities.GetIdByTitle(title, VietPremierMenu.TravelStyle, VietPremierDetailList.Detail);

                var random = new Random();

                GetStyleModel model = new GetStyleModel();
                EntityTravelStyle travelStyle = db.EntityTravelStyles.FirstOrDefault(o => o.IdTravelCity == idTravelCity && o.IdStyle == idStyle);
                EntityTravelCity travelCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == travelStyle.IdTravelCity);
                if (travelCity != null)
                {                    
                    EntityStyle style = db.EntityStyles.FirstOrDefault(o => o.Id == travelStyle.IdStyle);
                    if (style != null)
                    {
                        model.style.Id = style.Id;
                        model.style.Name = style.Name;
                    }
                    model.travelCity.Id = travelCity.Id;
                    model.travelCity.Name = travelCity.Name;
                    model.travelCity.Body = travelCity.Body;
                    model.travelCity.Itinerary = travelCity.Itinerary;
                    model.travelCity.Note = travelCity.Note;
                    model.travelCity.Image = travelCity.Image;
                    model.travelCity.Description = travelCity.Description;
                    model.travelCity.Price = travelCity.Price;
                    //model.travelCity.TripCode = travelCity.TripCode;
                    model.travelCity.Time = travelCity.Time;
                    model.travelCity.Duration = travelCity.Duration;

                    //SEO
                    model.travelCity.Titletag = travelCity.Titletag;
                    model.travelCity.Meta_description = travelCity.Meta_description;
                    model.travelCity.Meta_keyword = travelCity.Meta_keyword;

                    foreach (var listTravelStyle in db.EntityTravelStyles.Where(o => o.IdStyle == idStyle && o.IdTravelCity != travelCity.Id).Take(4))
                    {
                        EntityTravelCity listTravelCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == listTravelStyle.IdTravelCity);
                        model.listTravelCity.Add(new TravelCity()
                        {
                            Id = listTravelCity.Id,
                            Name = listTravelCity.Name,
                            Image = listTravelCity.Image,
                            Price = listTravelCity.Price,
                            //TripCode = listTravelCity.TripCode,
                            Time = listTravelCity.Time,
                            Duration = listTravelCity.Duration,
                            Description = listTravelCity.Description
                        });
                    }

                    return View(model);
                }
                return View();
            }

        }

        [HttpPost]
        public ActionResult TravelDetail(string title, string city, GetStyleModel model)
        {
            if (ModelState.IsValid)
            {
                using (dbVietPremierDataContext db = new dbVietPremierDataContext())
                {
                    int idTravelCity = Utilities.GetIdByTitle(city, VietPremierMenu.TravelCity, VietPremierDetailList.Detail);
                    int idStyle = Utilities.GetIdByTitle(title, VietPremierMenu.TravelStyle, VietPremierDetailList.Detail);

                    EntityTravelStyle travelStyle = db.EntityTravelStyles.FirstOrDefault(o => o.IdTravelCity == idTravelCity && o.IdStyle == idStyle);
                    EntityTravelCity travelCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == travelStyle.IdTravelCity);

                    StringBuilder mailBody = new StringBuilder();
                    mailBody.AppendFormat("<h1>Program Information</h1>");
                    mailBody.AppendFormat("<table><tr><td style='width:200px'>Tour:</td><td>" + model.travelCity.Name + "</td></tr>");
                    mailBody.AppendFormat("<tr style='height:20px'></tr> ");
                    mailBody.AppendFormat("<tr><td>Họ tên:</td><td>" + model.Gender + " " + model.FullName + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Đơn vị/ Doanh nghiệp:</td><td>" + model.Supplier + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Nơi đến:</td><td>" + model.Destination + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Quốc tịch:</td><td>" + model.Nationality + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Điện thoại:</td><td>" + model.Telephone + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Email:</td><td>" + model.Email + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Số lượng người lớn:</td><td>" + model.NumberofAdults + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Số lượng trẻ em:</td><td>" + model.NumberofChildren + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Ngày đi:</td><td>" + model.DateFrom + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Ngày về:</td><td>" + model.DateTo + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Chuẩn khách sạn:</td><td>" + model.Place + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Yêu cầu đặc biệt khác:</td><td>" + model.SpecialRequest + "</td></tr></table>");
                    mailBody.AppendFormat("<br></br>");
                    mailBody.AppendFormat("<p>Email được gửi từ một email ảo, No reply</p>");

                    try
                    {
                        MailHelper.SendMailMessage("tanqt.4.14.1990@gmail.com", "sales@vietnampremier.com", "hmduc@khainguyenepco.vn", "khoa@vietnampremier.com", "subject", mailBody.ToString(), "smtp.gmail.com", true, "tanqt.4.14.1990@gmail.com", "anhdao37");
                        EntityFeedBack feedback = new EntityFeedBack();
                        feedback.Email = model.Email;
                        feedback.DateCreate = DateTime.Now;
                        feedback.Body = mailBody.ToString();
                        feedback.Page = "Promotion";
                        db.EntityFeedBacks.InsertOnSubmit(feedback);
                        db.SubmitChanges();
                        model = new GetStyleModel();
                        if (travelCity != null)
                        {
                            EntityStyle style = db.EntityStyles.FirstOrDefault(o => o.Id == travelStyle.IdStyle);
                            if (style != null)
                            {
                                model.style.Id = style.Id;
                                model.style.Name = style.Name;
                            }
                            model.travelCity.Id = travelCity.Id;
                            model.travelCity.Name = travelCity.Name;
                            model.travelCity.Body = travelCity.Body;
                            model.travelCity.Itinerary = travelCity.Itinerary;
                            model.travelCity.Note = travelCity.Note;
                            model.travelCity.Image = travelCity.Image;
                            model.travelCity.Description = travelCity.Description;
                            model.travelCity.Price = travelCity.Price;
                            //model.travelCity.TripCode = travelCity.TripCode;
                            model.travelCity.Time = travelCity.Time;
                            model.travelCity.Duration = travelCity.Duration;

                            //SEO
                            model.travelCity.Titletag = travelCity.Titletag;
                            model.travelCity.Meta_description = travelCity.Meta_description;
                            model.travelCity.Meta_keyword = travelCity.Meta_keyword;

                            foreach (var listTravelStyle in db.EntityTravelStyles.Where(o => o.IdStyle == idStyle && o.IdTravelCity != travelCity.Id).Take(4))
                            {
                                EntityTravelCity listTravelCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == listTravelStyle.IdTravelCity);
                                model.listTravelCity.Add(new TravelCity()
                                {
                                    Id = listTravelCity.Id,
                                    Name = listTravelCity.Name,
                                    Image = listTravelCity.Image,
                                    Price = listTravelCity.Price,
                                    //TripCode = listTravelCity.TripCode,
                                    Time = listTravelCity.Time,
                                    Duration = listTravelCity.Duration,
                                    Description = listTravelCity.Description
                                });
                            }
                            TempData["Message"] = "Success";
                        }
                    }
                    catch
                    {
                        model = new GetStyleModel();
                        if (travelCity != null)
                        {
                            EntityStyle style = db.EntityStyles.FirstOrDefault(o => o.Id == travelStyle.IdStyle);
                        if (style != null)
                        {
                            model.style.Id = style.Id;
                            model.style.Name = style.Name;
                        }
                        model.travelCity.Id = travelCity.Id;
                        model.travelCity.Name = travelCity.Name;
                        model.travelCity.Body = travelCity.Body;
                        model.travelCity.Itinerary = travelCity.Itinerary;
                        model.travelCity.Note = travelCity.Note;
                        model.travelCity.Image = travelCity.Image;
                        model.travelCity.Description = travelCity.Description;
                        model.travelCity.Price = travelCity.Price;
                        //model.travelCity.TripCode = travelCity.TripCode;
                        model.travelCity.Time = travelCity.Time;
                        model.travelCity.Duration = travelCity.Duration;

                        //SEO
                        model.travelCity.Titletag = travelCity.Titletag;
                        model.travelCity.Meta_description = travelCity.Meta_description;
                        model.travelCity.Meta_keyword = travelCity.Meta_keyword;

                        foreach (var listTravelStyle in db.EntityTravelStyles.Where(o => o.IdStyle == idStyle && o.IdTravelCity != travelCity.Id).Take(4))
                        {
                            EntityTravelCity listTravelCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == listTravelStyle.IdTravelCity);
                            model.listTravelCity.Add(new TravelCity()
                            {
                                Id = listTravelCity.Id,
                                Name = listTravelCity.Name,
                                Image = listTravelCity.Image,
                                Price = listTravelCity.Price,
                                //TripCode = listTravelCity.TripCode,
                                Time = listTravelCity.Time,
                                Duration = listTravelCity.Duration,
                                Description = listTravelCity.Description
                            });
                        }
                        TempData["Message"] = "ErrorMessage";
                        }
                    }
                    return View(model);
                }
            }
            else
            {
                using (dbVietPremierDataContext db = new dbVietPremierDataContext())
                {
                int idTravelCity = Utilities.GetIdByTitle(city, VietPremierMenu.TravelCity, VietPremierDetailList.Detail);
                int idStyle = Utilities.GetIdByTitle(title, VietPremierMenu.TravelStyle, VietPremierDetailList.Detail);

                EntityTravelStyle travelStyle = db.EntityTravelStyles.FirstOrDefault(o => o.IdTravelCity == idTravelCity && o.IdStyle == idStyle);
                EntityTravelCity travelCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == travelStyle.IdTravelCity);
                if (travelCity != null)
                {
                    EntityStyle style = db.EntityStyles.FirstOrDefault(o => o.Id == travelStyle.IdStyle);
                    if (style != null)
                    {
                        model.style.Id = style.Id;
                        model.style.Name = style.Name;
                    }
                    model.travelCity.Id = travelCity.Id;
                    model.travelCity.Name = travelCity.Name;
                    model.travelCity.Body = travelCity.Body;
                    model.travelCity.Itinerary = travelCity.Itinerary;
                    model.travelCity.Note = travelCity.Note;
                    model.travelCity.Image = travelCity.Image;
                    model.travelCity.Description = travelCity.Description;
                    model.travelCity.Price = travelCity.Price;
                    //model.travelCity.TripCode = travelCity.TripCode;
                    model.travelCity.Time = travelCity.Time;
                    model.travelCity.Duration = travelCity.Duration;

                    //SEO
                    model.travelCity.Titletag = travelCity.Titletag;
                    model.travelCity.Meta_description = travelCity.Meta_description;
                    model.travelCity.Meta_keyword = travelCity.Meta_keyword;

                    foreach (var listTravelStyle in db.EntityTravelStyles.Where(o => o.IdStyle == idStyle && o.IdTravelCity != travelCity.Id).Take(4))
                    {
                        EntityTravelCity listTravelCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == listTravelStyle.IdTravelCity);
                        model.listTravelCity.Add(new TravelCity()
                        {
                            Id = listTravelCity.Id,
                            Name = listTravelCity.Name,
                            Image = listTravelCity.Image,
                            Price = listTravelCity.Price,
                            //TripCode = listTravelCity.TripCode,
                            Time = listTravelCity.Time,
                            Duration = listTravelCity.Duration,
                            Description = listTravelCity.Description
                        });
                    }
                    TempData["ContactUs"] = "Error";
                    return View(model);
                }
                    return View(model);
                }
            }
        }
        #endregion
    }
}
